<template>
  <el-card shadow="never">
    <div slot="header">
      项目申报审核(科室)
      <el-button
        type="primary"
        size="small"
        class="fr"
        @click="handleUpdateLot(1)"
        :disabled="selectedRows.length === 0"
      >审核通过</el-button>
      <el-button
        type="danger"
        size="small"
        :disabled="selectedRows.length === 0"
        class="fr mr10"
        @click="handleUpdateLot(3)"
      >审核不通过</el-button>
    </div>
    <e-table
      ref="eTable"
      @tableCheck="handleTableCheck"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="selection" align="center" type="selection" width="55" />
      <el-table-column label="附件图片" slot="savePath" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.savePath && scope.row.savePath.length > 0">
            <el-button
              type="text"
              @click="handlePreview(scope.row)"
            >{{ scope.row.savePath.length }}张</el-button>
          </template>
          <template v-else>无</template>
        </template>
      </el-table-column>
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleUpdate(1, scope.row)">
                <a>审核通过</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleUpdate(3, scope.row)">
                <a class="red">审核不通过</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
    <preview-modal ref="previewModal" />
  </el-card>
</template>

<script>
import { getDeptExamine, getUserExamine, applyDelete, updateUserExamine, updateUserExamineList } from '@/api/eval'
import { openNotification } from '@/utils/util'
import PreviewModal from '@/components/PreviewModal'

export default {
  components: {
    PreviewModal
  },
  data() {
    return {
      selectedRows: [],
      tableData: [],
      columns: [
        {
          slot: 'selection'
        },
        {
          label: '标题',
          prop: 'applyTitle'
        },
        {
          label: '项目名称',
          prop: 'evalItemName'
        },
        {
          label: '考评标准',
          prop: 'standard'
        },
        {
          label: '日期',
          prop: 'eventDate'
        },
        {
          label: '奖惩形式',
          prop: 'rewardType',
          formatter(row, column, cellValue, index) {
            return cellValue === 1 ? '奖' : ' 惩'
          }
        },
        {
          label: '奖惩值',
          prop: 'rewardValue'
        },
        {
          label: '责任科室',
          prop: 'targetDept'
        },
        {
          slot: 'savePath'
        },
        // {
        //   label: '附件图片',
        //   prop: 'photoList'
        // },
        {
          label: '事件描述',
          prop: 'eventDescribe'
        },
        {
          slot: 'action'
        }
      ],
      loading: false,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      total: 1
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handlePreview(record) {
      this.$refs.previewModal.show(record.savePath)
    },
    async handleUpdateLot(auditState) {
      this.selectedRows.map(v => {
        v.auditState = auditState
      })
      let mes = null
      if (auditState === 1) {
        mes = '批量审核通过操作成功'
      } else {
        mes = '批量审核不通过操作成功'
      }
      const res = await updateUserExamineList(this.selectedRows)
      if (res.state === '000000') {
        openNotification('success', mes)
        this.handleGetList()
      }
    },
    async handleUpdate(auditState, row) {
      let param = { ...row, auditState }
      const res = await updateUserExamine(param)
      if (res.state === '000000') {
        openNotification('success', '审核操作成功')
        this.handleGetList()
      }
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条申报记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await applyDelete(row.evalApplyDetailId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await getUserExamine({ ...this.queryParam, ...param })
      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleTableCheck(arr) {
      this.selectedRows = arr
    }
  }
}
</script>
